import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;

import local.util.MyPrint;

public class FindSectorAndIndustryName {    

    private static boolean save = false;
    private static boolean debug = false;
    private String myUrl = null;
    private String urlReturnString = new String();
    private Vector urlReturnVector = new Vector(10);
    private String startString  = null;
    private String endString    = null;
    private static String sectorName   = null;
    private static String industryName = null;

    public static void main (String[] args) {

	if (args.length <1 ) {
	    MyPrint.printLine("=",50);
	    System.out.println("This application take stock tick as input");
	    System.out.println("Find sector and industry name from yahoo");
	    System.out.println("If this tick is not in database, it will insert");
	    MyPrint.printLine("=",50);
	    System.exit(0);
	}

	String query_string = null;
	String sector_id    = null;
	String industry_id  = null;
 
	Hashtable sector_hashtable = new Hashtable();
	Hashtable industry_hashtable = new Hashtable();
	Hashtable tick_db_hashtable = new Hashtable();

	DataBaseQuery db = new DataBaseQuery ();

	query_string = "SELECT * FROM Sector";

	db.setSectorHashtable(query_string);
	sector_hashtable = db.getSectorHashtable();

	query_string = "SELECT * FROM Industry";
	db.setIndustryHashtable(query_string);

	industry_hashtable = db.getIndustryHashtable();

	query_string = "SELECT * FROM Tick";
	db.setTickIndustryIdHashtable(query_string);
	tick_db_hashtable = db.getTickIndustryIdHashtable();

	Hashtable tick_hashtable = new Hashtable ();

	String tick = args[0];

	File file_name_f = new File ("./Data/"+tick);

	FindSectorAndIndustryName fs = new FindSectorAndIndustryName();

	if (file_name_f.isFile()) {
	
	    System.out.println("Loading file......."+ file_name_f.getName());
	  
	    TickFileLoader t_f_loader = new TickFileLoader();
	    t_f_loader.setFileName(file_name_f);
	    t_f_loader.loadFile();
	    tick_hashtable = t_f_loader.getHashTable();
	    System.out.println("Total Loaded :"+ tick_hashtable.size()+"   Stocks!");

	    Enumeration enum = tick_hashtable.keys();  
	
	    while (enum.hasMoreElements()) {
		tick= (String)enum.nextElement();

		if (fs.printTickNotInDb (tick_db_hashtable, tick)) {
		    continue;
		}

		//System.out.println(tick);
		fs.getSectorAndIndustryInfo(tick);

		if ( sectorName != null && industryName != null) {

		    sector_id = (String)sector_hashtable.get(sectorName);
		    System.out.println("sector_id="+sector_id);
			  
		    industry_id = (String)industry_hashtable.get(industryName);
		    System.out.println("industry_id="+industry_id);

		    query_string = "INSERT INTO Tick VALUES('"+tick+"',"+
			"'"+industryName+"',"+industry_id+","+
			"'"+sectorName+"',"+sector_id+");";
		    fs.insertIntoDataBase(query_string);
		}
		else {
		    System.out.println("Can not find Sector Name and Industry Name for : "+tick+"*********");
		    continue;
		}
	    }
 	}
	else {
	    debug = true;
	    System.out.println("Finding Sector and Industry Name for: "+ args[0]+".....");
	    fs.getSectorAndIndustryInfo(tick);

	    sector_id = (String)sector_hashtable.get(sectorName);
	    //System.out.println("sector_id="+sector_id);
			  
	    industry_id = (String)industry_hashtable.get(industryName);
	    //System.out.println("industry_id="+industry_id);
	    query_string = "INSERT INTO Tick VALUES('"+tick+"',"+
		"'"+industryName+"',"+industry_id+","+
		"'"+sectorName+"',"+sector_id+");";
	    fs.insertIntoDataBase(query_string);
	    //System.out.println(query_string);
	}
    }

    private boolean  printTickNotInDb (Hashtable tick_hashtable, String tick) {
	if (tick_hashtable.containsKey(tick)) {
	    // System.out.println("Found tick: "+tick +" in database");
	    return true;
	}
	else {
	    System.out.println(tick);
	    return false;
	}
    }

    private void getSectorAndIndustryInfo(String tick) {
	String sector_name   = null;
	String industry_name = null;
	String sector_id     = null;
	String industry_id   = null;

	String url_return_string = null;
	Vector url_return_vector = new Vector ();
	char char_tick[] = tick.toCharArray();

	HttpDataLoader mydl = new HttpDataLoader ();
	mydl.setUrl("http://biz.yahoo.com/p/"+char_tick[0]+"/"+tick+".html");
	mydl.connectToUrl();
	url_return_string = mydl.getUrlReturnString();
	
	url_return_vector = mydl.stringToVector (url_return_string);
	    
	mydl.setStartString("Competitors:");
	mydl.setEndString("Company Website");
	url_return_vector = mydl.selectUsefulElements (url_return_vector);
	mydl.setStartString("Competitors:");
	mydl.setEndString("Related Links");
	url_return_vector = mydl.selectUsefulElements (url_return_vector);

	if (debug)
	    MyPrint.printVector(url_return_vector, 1);

	url_return_vector = mydl.deselectElement (url_return_vector, "&#183");
	url_return_vector = mydl.deselectElement (url_return_vector, ":");
	url_return_vector = mydl.deselectElement (url_return_vector, "(Specialty)");
	url_return_vector = mydl.deselectElement (url_return_vector, "Competitors");

	if (debug)
	    MyPrint.printVector(url_return_vector, 1);

	Enumeration enum = url_return_vector.elements();
	while (enum.hasMoreElements()) {
	    enum.nextElement();
	    sector_name = ((String)enum.nextElement()).replaceAll(" ","");
	    enum.nextElement();
	    industry_name = ((String)enum.nextElement()).replaceAll(" ","");
	    break;
	}
	
	
	System.out.println("Sector Name:"+ sector_name);
	System.out.println("Industry Name:"+ industry_name);

	setSectorName(sector_name);
	setIndustryName(industry_name);
    }

    private void setSectorName (String sector_name) {
	sectorName = sector_name;
    }

    private void setIndustryName(String industry_name) {
	industryName = industry_name;
    }

    public void insertIntoDataBase( String query_string) {

	DataBaseInsert myInsert = new DataBaseInsert();	
	
	myInsert.setQueryString(query_string);
	myInsert.insertData();

    }         
}








